﻿
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using iText.Kernel.Pdf;
using System.Text;

namespace TextExtractionMaster.Extractor
{

    public class PdfTextExtractor : ITextExtractor
    {
        public string ExtractText(string filePath)
        {
            StringBuilder text = new StringBuilder();

            using (PdfReader reader = new PdfReader(filePath))
            using (PdfDocument pdfDoc = new PdfDocument(reader))
            {
                int numberOfPages = pdfDoc.GetNumberOfPages();

                for (int i = 1; i <= numberOfPages; i++)
                {
                    text.Append(GetTextFromPage(pdfDoc, i));
                }
            }

            return text.ToString();
        }

        private string GetTextFromPage(PdfDocument pdfDoc, int pageNumber)
        {
            ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
            return GetTextFromPage(pdfDoc.GetPage(pageNumber), strategy);
        }

        private string GetTextFromPage(PdfPage pdfPage, ITextExtractionStrategy strategy)
        {
            return GetTextFromPage(pdfPage, strategy);
        }

    }

}
